package tech.abing.spider.task.pageprocessor;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.constants.FinalConstants;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.ComboInfo;
import tech.abing.spider.task.model.Product;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.SysUtil;



public class TBProductPageProccessor1 implements PageProcessor {

	static Pattern normalContain = Pattern.compile("淘宝");//正常页面必定会存在的字符，你也可以使用页面上其他的字符
	
	private static Logger logger = Logger.getLogger(TBProductPageProccessor1.class);
	
	static AtomicInteger atomicInteger = new AtomicInteger(0);
	
	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return normalContain;
	}

	@Override
	public void process(OkPage page, StartContext context, List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		
		int count = atomicInteger.incrementAndGet();
		
		String itemId = page.getRequest().getParamsByName("itemId").toString();
		String shopId = page.getRequest().getParamsByName("shopId").toString();
		String shopType = page.getRequest().getParamsByName("shopType").toString();
		
		logger.info("淘宝采集成功产品数："+count);
		
		Document doc = Jsoup.parse(page.getContent());
		
		Product prod = new Product();
		
		prod.setId(itemId);
		prod.setPrdNo(itemId);
		prod.setUrl(page.getRequest().getUrl());
		prod.setShopId(shopId);
		prod.setCatchDate(DateUtils.getDateTime());
		prod.setShopType(shopType);
		
		
		String picUrl = doc.select("img[id=J_ImgBooth]").attr("src");
		if("".equals(picUrl)){
			picUrl = doc.select("img[id=J_ImgBooth]").attr("data-src");
		}
		if(picUrl.indexOf("https")==-1){
			picUrl = "https:"+picUrl;
		}
		prod.setImgUri(picUrl);
		
		prod.setCat(doc.select("div[id=J_Pine]").attr("data-catid"));
		prod.setPrdName(doc.select("h3[class=tb-main-title]").attr("data-title"));
		
		String sellerId = doc.select("meta[name=microscope-data]").attr("content");
		if(sellerId.contains("userid=")){
			sellerId = sellerId.substring(sellerId.indexOf("userid=")+7);
			sellerId = sellerId.substring(0,sellerId.indexOf(";"));
		}
		prod.setSellerId(sellerId);
		
		//套餐组合
		String comboApi = "";
		comboApi = doc.select("div[id=promote]").attr("data-default");
		if(!"".equals(comboApi)){
			comboApi = "https:"+comboApi;
			saveComboInfo(comboApi, objectContainer, shopId,sellerId);
		}
		
		//收藏人气
		//prod.setCollectNum(getCollectNum(itemId));
		//评论量
		prod.setComments(getComments(itemId));
		
		Elements eles = doc.select("script");
		String arr[] =null;
		String temp = "", priceUrl = "", saleCountUrl = "";
		for(Element x:eles){
			if(x.html().contains("Hub.config.set")){
				temp = x.html();
				temp = temp.replace("\"", "");
				arr = temp.split(",");
				for(String str:arr){
					if(str.contains("wholeSibUrl")){
						priceUrl = str.split(":")[1];
						if(!priceUrl.contains("http")){
							priceUrl="https:"+priceUrl;
						}
					}
					if(str.contains("apiItemInfo")){
						saleCountUrl = str.split(":")[1];
						if(!saleCountUrl.contains("http")){
							saleCountUrl="https:"+saleCountUrl;
						}
					}
					if(!"".equals(priceUrl) && !"".equals(saleCountUrl)){
						break;
					}
				}
				break;
			}
		}
		
		prod.setMonthSale(getMonthSale(saleCountUrl));
		
		//价格信息获取
		prod.setPrice(doc.select("strong[id=J_StrPrice] em[class=tb-rmb-num]").text());
		//活动价格
		arr = getPriceInfo(priceUrl, itemId);
		if(arr != null && arr.length == 2){
			prod.setPromoType(arr[0]);
			prod.setProPrice(arr[1]);
		}
		
		//参数
		String param = "", brand = "";
		eles = doc.select("ul[class=attributes-list] li");
		for(Element x:eles){
			param += "@@"+x.text();
			if("".equals(brand)){
				if(x.text().contains("品牌")){
					brand = x.text();
				}
			}
		}
		prod.setParam(param);
		prod.setBrand(brand);
		
		prod.setShopName(doc.select("div[class=tb-shop-name] a").text());
		String shopUrl =  doc.select("div[class=tb-shop-name] a").attr("href");
		if(shopUrl.indexOf("http")==-1){
			shopUrl = "http"+shopUrl;
		}
		prod.setShopUrl(shopUrl);
		
		objectContainer.add(prod);
		
	}

	private void saveComboInfo(String comboApi,
			List<Proccessable> objectContainer, String shopId,String sellerId) {
		Document doc = null;
		Elements eles = null;
		String temp = "";
		String mealId = "";
		String mealName = "";
		ComboInfo combo = null;
		try {
			doc = Jsoup.connect(comboApi).timeout(FinalConstants.JSOUP_TIME_OUT).get();
			eles = doc.select("ol[id=J_BundlePage] li");
			String arr[] = null;
			for(Element x:eles){
				temp = x.attr("data-mealdata");
				arr = temp.split("\\|");
				mealId = arr[0];
				String[] items = arr[1].split(",");
				mealName = arr[4];
				for(String itemId:items){
					combo = new ComboInfo();
					combo.setId(SysUtil.getUUID());
					combo.setMealId(mealId);
					combo.setItemId(itemId);
					combo.setSaleName(mealName);
					combo.setShopId(shopId);
					combo.setCatchDate(DateUtils.getDate());
					objectContainer.add(combo);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

	private String getMonthSale(String saleCountUrl) {
		Document doc = null;
		String result = "";
		try {
			doc = Jsoup.connect(saleCountUrl).timeout(FinalConstants.JSOUP_TIME_OUT).ignoreContentType(true).get();
			result = doc.text();
			
			//{dummy:false ,quantity:{ interval: 30 ,quanity:3143 ,confirmGoods:2392 } ,tadContent: {} }
			result = result.substring(result.indexOf("(")+1, result.indexOf(")"));
			
			JSONObject jsonObj = JSONObject.fromObject(result);
			
			result = jsonObj.getJSONObject("quantity").getString("confirmGoods");
			
		} catch (Exception e) {
			logger.error("=========>月销量获取失败:", e);
		}
		
		return result;
	}
	
	private String getComments(String prodId) {
		Document doc = null;
		String result = "";
		try {
			doc = Jsoup.connect("https://count.taobao.com/counter3?keys=ICE_3_feedcount-"+prodId+"&callback=jsoup").timeout(FinalConstants.JSOUP_TIME_OUT).ignoreContentType(true).get();
			result = doc.text();
			if(result.contains(":")){
				result = result.split(":")[1].trim();
			}
			result = result.replace("}", "").replace(")", "").replace(";", ""); 
		} catch (Exception e) {
			logger.error("=========>评论量获取失败:", e);
		}
		
		return result;
	}
	
	private String getCollectNum(String prodId) {
		Document doc = null;
		String result = "";
		try {
			doc = Jsoup.connect("https://count.taobao.com/counter3?keys=ICCP_1_"+prodId+"&callback=jsoup").timeout(FinalConstants.JSOUP_TIME_OUT).ignoreContentType(true).get();
			result = doc.text();
			if(result.contains(":")){
				result = result.split(":")[1].trim();
			}
			result = result.replace("}", "").replace(")", "").replace(";", ""); 
		} catch (Exception e) {
			logger.error("=========>收藏人气获取失败:", e);
		}
		return result;
	}
	
	private String[] getPriceInfo(String priceUrl, String prodId) {
		Document doc = null;
		String temp = null;
		JSONObject jsonObject = null;
		String price = null;
		String type = null;
		String[] result=null;
		try {
			org.jsoup.Connection con=Jsoup.connect(priceUrl).timeout(FinalConstants.JSOUP_TIME_OUT);//获取连接
			con.header("Accept-Encoding", "gzip, deflate");   
			con.header("Cookie", "isg=6CAF6BB50B4A75EC01CF1AC5F05C3C27; t=3e563e3c9f8000a0e002f31ac22e9225; thw=cn; cna=13n9DSWK2HICAdoSj3hmw03j; l=AnV1I2hqtPZmAP2IZ0p0ql/1pfsvqikE; uc3=nk2=oYnxK3W6prGnxhuisQ%3D%3D&id2=UojcjGfJbikI&vt3=F8dASM%2FCdO9F7T3AwjA%3D&lg2=UIHiLt3xD8xYTw%3D%3D; tracknick=%5Cu8DEF%5Cu4FA0%5Cu69D0%5Cu537F_%5Cu8D63%5Cu5DDE; _cc_=VFC%2FuZ9ajQ%3D%3D; tg=0; x=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D-1%26_ato%3D0; ucn=center; ck1=; lgc=%5Cu8DEF%5Cu4FA0%5Cu69D0%5Cu537F_%5Cu8D63%5Cu5DDE; v=0; cookie2=1c05f5867338bfd5e9b6e5db67fa0bcb; _tb_token_=5167733bfdb13; uc1=lltime=1438046917&cookie14=UoW0Gpy6V8FLHg%3D%3D&existShop=false&cookie16=VT5L2FSpNgq6fDudInPRgavC%2BQ%3D%3D&cookie21=U%2BGCWk%2F7owY3i1vB1W2BgQ%3D%3D&tag=2&cookie15=Vq8l%2BKCLz3%2F65A%3D%3D&pas=0; mt=ci=0_1; existShop=MTQzODA1MjA2Ng%3D%3D; sg=%E5%B7%9E7a; cookie1=AnQL98gU%2B2%2BvitMdKJ2D87DwY7lFJ9qLgEqhVYh5f0w%3D; unb=198731927; skt=aabe86c3fac112d8; publishItemObj=Ng%3D%3D; _l_g_=Ug%3D%3D; _nk_=%5Cu8DEF%5Cu4FA0%5Cu69D0%5Cu537F_%5Cu8D63%5Cu5DDE; cookie17=UojcjGfJbikI; whl=-1%260%260%260");
			con.header("Host", "detailskip.taobao.com");
			con.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0");
			con.header("Accept", "*/*");
			con.header("Host", "detailskip.taobao.com");
			con.header("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
			con.header("Accept-Encoding", "gzip, deflate");
			con.header("Referer", "https://item.taobao.com/item.htm?spm=a1z10.3-c.w4002-11208447118.25.EVTttE&id="+prodId);
			con.header("Connection", "keep-alive");
			doc=con.ignoreContentType(true).get();
			temp = doc.text();
			
			if(StringUtils.isNotBlank(temp) && temp.indexOf("\"def\":[ {") > -1){
				temp = temp.substring(temp.indexOf("\"def\":[ {") + 8);
				temp = temp.substring(0, temp.indexOf("}") + 1);
				
				jsonObject = JSONObject.fromObject(temp);
				
				if(jsonObject.containsKey("type")){
					type = jsonObject.getString("type");
				}
				
				if(jsonObject.containsKey("price")){
					price = jsonObject.getString("price");
				}
				
				result = new String[]{type,price};
			}
			
		} catch (Exception e) {
			logger.error("=========>活动价格失败:", e);
		} 
		return result;
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}
	

}
